const merge = require('webpack-merge')
const common = require('./webpack.common')
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin")
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin')

module.exports = merge.default(common, {
    mode: 'production',
    optimization: {
        usedExports: true,
        minimize: true,
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendor',
                    chunks: 'all'
                }
            }
        }
    },
    module: {
        rules: [{
            test: /\.css?$/,
            use: [MiniCssExtractPlugin.loader, 'css-loader']
        }]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, './public'),
                to: path.resolve(__dirname, './dist/public'),
                toType: 'dir'
            }]
        }),
        new MiniCssExtractPlugin({
            filename: "main-[hash:8].bundle.css"
        })
    ]
})